<?php
/**
 * This file is part of DomSql.
 * 
 * DomSql is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * DomSql is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with DomSql.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * @link http://inditel.ee/
 * @copyright 2007-2009 Inditel Meedia OÜ
 * @author Oliver Leisalu <oliver@inditel.ee>
 */

/**
* Class that containts methods for faster unit testing.
* @package DomSql
*/
class DomSqlUnitTestCase extends UnitTestCase {
	
	final public function assertDbValue( $table, $field, $id, $expectedValue, $message = NULL ) {
		$r = DomSql::selectOneQuery('SELECT `'.addslashes($field).'` FROM '.addslashes($table).' WHERE id = '.(int)$id, $field );
		if( $message == NULL ) {
			$message = 'Excpected ['.substr($expectedValue,0,40).'] but found ['.substr($r,0,40).'] from database!';
		}
		
		return $this->assert( new TrueExpectation(), $r == $expectedValue, $message);
	}
	
	final public function assertDbValueNull( $table, $field, $id, $message = NULL ) {
		$r = DomSql::selectOneQuery('SELECT id, `'.addslashes($field).'` FROM '.addslashes($table).' WHERE id = '.(int)$id.' AND `'.addslashes($field).'` <=> NULL LIMIT 1' );
		if( $message == NULL ) {
			$message = 'Excpected [NULL] but found ['.substr($r[$field],0,40).'] from database!';
		}
		return $this->assert( new TrueExpectation(), (bool)$r['id'], $message);
	}

	final public function assertDbTableEmpty( $tableName, $message = NULL ) {
		try {
			$r = DomSql::selectOneQuery('SELECT count(*) as n FROM '.addslashes($tableName).'','n');
		} catch( Exception $e ) {
			$r = 0;
		}
		return $this->assert( new TrueExpectation(), $r == 0, $message);
	}
	
}
?>